package com.ruoyi.message.structures.service.impl;

import java.util.List;

import com.ruoyi.message.structures.service.IPaperStructuresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.types.domain.PaperTypes;
import com.ruoyi.message.structures.mapper.PaperStructuresMapper;
import com.ruoyi.message.structures.domain.PaperStructures;

/**
 * 结构步骤Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-09
 */
@Service
public class PaperStructuresServiceImpl implements IPaperStructuresService
{
    @Autowired
    private PaperStructuresMapper paperStructuresMapper;

    /**
     * 查询结构步骤
     * 
     * @param structureId 结构步骤主键
     * @return 结构步骤
     */
    @Override
    public PaperStructures selectPaperStructuresByStructureId(Long structureId)
    {
        return paperStructuresMapper.selectPaperStructuresByStructureId(structureId);
    }

    /**
     * 查询结构步骤列表
     * 
     * @param paperStructures 结构步骤
     * @return 结构步骤
     */
    @Override
    public List<PaperStructures> selectPaperStructuresList(PaperStructures paperStructures)
    {
        return paperStructuresMapper.selectPaperStructuresList(paperStructures);
    }

    /**
     * 新增结构步骤
     * 
     * @param paperStructures 结构步骤
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPaperStructures(PaperStructures paperStructures)
    {
        int rows = paperStructuresMapper.insertPaperStructures(paperStructures);
        insertPaperTypes(paperStructures);
        return rows;
    }

    /**
     * 修改结构步骤
     * 
     * @param paperStructures 结构步骤
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePaperStructures(PaperStructures paperStructures)
    {
        paperStructuresMapper.deletePaperTypesByStructureId(paperStructures.getStructureId());
        insertPaperTypes(paperStructures);
        return paperStructuresMapper.updatePaperStructures(paperStructures);
    }

    /**
     * 批量删除结构步骤
     * 
     * @param structureIds 需要删除的结构步骤主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePaperStructuresByStructureIds(Long[] structureIds)
    {
        paperStructuresMapper.deletePaperTypesByStructureIds(structureIds);
        return paperStructuresMapper.deletePaperStructuresByStructureIds(structureIds);
    }

    /**
     * 删除结构步骤信息
     * 
     * @param structureId 结构步骤主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePaperStructuresByStructureId(Long structureId)
    {
        paperStructuresMapper.deletePaperTypesByStructureId(structureId);
        return paperStructuresMapper.deletePaperStructuresByStructureId(structureId);
    }

    /**
     * 新增类型信息
     * 
     * @param paperStructures 结构步骤对象
     */
    public void insertPaperTypes(PaperStructures paperStructures)
    {
        List<PaperTypes> paperTypesList = paperStructures.getPaperTypesList();
        Long structureId = paperStructures.getStructureId();
        if (StringUtils.isNotNull(paperTypesList))
        {
            List<PaperTypes> list = new ArrayList<PaperTypes>();
            for (PaperTypes paperTypes : paperTypesList)
            {
                paperTypes.setStructureId(structureId);
                list.add(paperTypes);
            }
            if (list.size() > 0)
            {
                paperStructuresMapper.batchPaperTypes(list);
            }
        }
    }
}
